package com.victor.controller;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.JMInfo;
import com.victor.entity.UserFeedback;
import com.victor.entity.UserFeedbackClass;
import com.victor.service.MyselfService;
import com.victor.utils.Msg;

/**
 * 问题反馈模块
 * @author qinyuquan
 * 
 */
@RestController
@RequestMapping("/backProblem")
public class FeedbackProblemController {
	
	@Autowired
	private MyselfService service;
	
	/**
	 * 后台添加反馈类型
	 * @param userFeedbackClass
	 * @return
	 */
	@RequestMapping("addBackClass")
	public Msg addBackClass(UserFeedbackClass userFeedbackClass) {
		if (userFeedbackClass==null || userFeedbackClass.getClassname()==null || userFeedbackClass.equals("")) {
			return Msg.fail("参数错误");
		}
		boolean b = service.addBackClass(userFeedbackClass);
		return Msg.judge(b);
	}
	
	/**
	 * 后台回复反馈信息
	 * @param id  反馈信息id
	 * @param replyid	反馈回复人id
	 * @param replymessage	反馈回复信息
	 * @return
	 */
	@RequestMapping("/replyFeedback")
	public Msg replyFeedback(Integer id,String replyid,String replymessage) {
		if (id==null || replyid==null || replymessage==null) {
			return Msg.fail("参数错误");
		}
		boolean b = service.replyFeedback(id,replyid,replymessage);
		return Msg.judge(b);
	}
	
	/**
	 * 后台删除反馈类型和该类型下所有的用户反馈
	 * @param id 反馈类型表id
	 */
	@RequestMapping("delBackClass")
	public Msg delBackClassById(Integer id) {
		if(id==null) {
			return Msg.fail("参数错误");
		}
		boolean b = service.delBackClassById(id);
		return Msg.judge(b);
	}
	
	/**
	 * 	根据传入userFeedback字段查询列表
	 * @param userFeedback
	 * @return
	 */
	@RequestMapping("selectBack")
	public Msg selectBackByClass(UserFeedback userFeedback) {
		if (userFeedback==null) {
			return Msg.fail("参数错误");
		}
		List<UserFeedback> list = service.selectBack(userFeedback);
		return Msg.success().add("list", list);
	}
	
	/**
	 * 根据反馈ids，批量删除反馈信息
	 * @param ids
	 * @return
	 */
	@RequestMapping("/batchDelete")
	public Msg batchDelete(int[] ids) {
		if (ids==null || ids.length==0) {
			return Msg.fail("参数错误");
		}
		boolean b = service.batchDelete(ids);
		if (b) {
			return Msg.success();
		}
		return Msg.fail();
	}
	
	
	/**
	 * 根据传入反馈信息字段查询反馈信息
	 * 可根据反馈问题的状态查询反馈信息
	 * @param UserFeedback   
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/batchExport")
	public void batchExport(UserFeedback userFeedback,HttpServletRequest request,HttpServletResponse response) throws Exception {
		//数据库查询数据 exportlist 需要导出的list
		List<UserFeedback> list = service.selectBack(userFeedback);
		// 创建poi导出数据对象
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		// 创建sheet页
		Sheet sheet = sxssfWorkbook.createSheet("用户反馈列表");

		CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 8);
		// 参数1：起始行 参数2：终止行 参数3：起始列 参数4：终止列
		sheet.addMergedRegion(region1);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("用户反馈信息");

		// 创建表头
		Row headRow = sheet.createRow(5);
		// 设置表头信息
		headRow.createCell(0).setCellValue("反馈编号");
		headRow.createCell(1).setCellValue("提交用户id");
		headRow.createCell(2).setCellValue("反馈类型id");
		headRow.createCell(3).setCellValue("反馈信息");
		headRow.createCell(4).setCellValue("反馈状态");//0已提交，1已回复
		headRow.createCell(5).setCellValue("回复人id");
		headRow.createCell(6).setCellValue("回复信息");

		// 遍历上面数据库查到的数据
		for (UserFeedback u : list) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(u.getId());
			dataRow.createCell(1).setCellValue(u.getUserid());
			dataRow.createCell(2).setCellValue(u.getClassid());
			dataRow.createCell(3).setCellValue(u.getMessage());
			dataRow.createCell(4).setCellValue(u.getStatus().equals("0") ? "已提交" : "已回复");
			dataRow.createCell(5).setCellValue(u.getReplyid());
			dataRow.createCell(6).setCellValue(u.getReplymessage());
		}
		// 下载导出文件名
		String filename = "用户反馈列表";
		// 设置头信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		// 一定要设置成xlsx格式
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		// 创建一个输出流
		ServletOutputStream outputStream = response.getOutputStream();
		// 写入数据
		sxssfWorkbook.write(outputStream);
		// 关闭
		outputStream.close();
		sxssfWorkbook.close();
	}

}
